package com.atguigu.flink04;

import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.ConnectedStreams;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.co.CoMapFunction;

/**
 * @author Felix
 * @date 2024/2/22
 * 该案例演示了合流算子
 * union
 *      可以对多条流进行合并
 *      参与合并的流数据类型必须一致
 */
public class Flink06_Connect {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.createLocalEnvironmentWithWebUI(new Configuration());
        env.setParallelism(1);

        //从8888端口读取数据
        DataStreamSource<String> ds1 = env.socketTextStream("hadoop102", 8888);
        //从8889端口读取数据
        SingleOutputStreamOperator<Integer> ds2 = env.socketTextStream("hadoop102", 8889).map(Integer::parseInt);

        ConnectedStreams<String, Integer> connectDS = ds1
                .connect(ds2);
        connectDS
                .map(
                        new CoMapFunction<String, Integer, String>() {
                            @Override
                            public String map1(String value) throws Exception {
                                return "字符串:" + value;
                            }

                            @Override
                            public String map2(Integer value) throws Exception {
                                return "整数:" + value;
                            }
                        }
                ).print();

        env.execute();

    }
}
